package com.gj.demojuc.chap01;
//资源类
class Ticket{
    private int number = 50;
    public synchronized void sale(){
        if (number <= 0){
            System.out.println("票卖光了");
            return;
        }
        try {
            System.out.println(Thread.currentThread().getName() + "开始售票共有"+number);
            Thread.sleep(200);
            number--;
            System.out.println(Thread.currentThread().getName() + "卖出一张剩余"+number);
        }catch (Exception e){
            throw new RuntimeException(e);
        }

    }
}

public class SaleTickets {
    public static void main(String[] args) {
        //资源对象
        Ticket ticket = new Ticket();
        //一个线程代表一个窗口
        new Thread(()->{
            //操作
            for (int i = 0; i < 50; i++) {
                ticket.sale();
            }
        },"thread 1").start();

        new Thread(()->{
            for (int i = 0; i < 50; i++) {
                ticket.sale();
            }
        },"thread 2").start();

        new Thread(()->{
            for (int i = 0; i < 50; i++) {
                ticket.sale();
            }
        },"thread 3").start();
    }
}
